package com.cxs.app.uilib.bar;

import com.cxs.app.cptlib.IMIcon;
import com.cxs.app.uilib.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * 自定义标题栏
 * <ol>
 *     <li>左侧返回图标</li>
 *     <li>中间标题内容</li>
 *     <li>
 *         <p>右侧图标按钮组</p>
 *         <ol>
 *             <li>左侧收缩图标</li>
 *             <li>右侧多选图标</li>
 *         </ol>
 *     </li>
 * </ol>
 */
public class IMTitleBar extends DirectionalLayout {

    private DirectionalLayout dLayout;
    // 返回按钮
    private IMIcon backIcon = null;
    // 标题
    private Text title = null;
    // 搜索按钮
    private IMIcon searchIcon = null;
    // 菜单按钮
    private IMIcon menuIcon = null;

    // 返回按钮点击事件
    private BackClickedListener backClickedListener;
    // 搜索按钮点击事件
    private SearchClickedListener searchClickedListener;
    // 菜单按钮点击事件

    public IMTitleBar(Context context) {
        super(context);
        initImCustomTitleBar(context, null);
        initEvent();
    }

    public IMTitleBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initImCustomTitleBar(context, attrSet);
        initEvent();
    }

    public IMTitleBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initImCustomTitleBar(context, attrSet);
        initEvent();
    }

    private void initImCustomTitleBar(Context context, AttrSet attrSet) {
        // 动态加载Layout
        Component component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_title, null, false);
        addComponent(component);

        // 子组件
        dLayout = (DirectionalLayout)component.findComponentById(ResourceTable.Id_layout);
        backIcon = (IMIcon)component.findComponentById(ResourceTable.Id_back_icon);
        title = (Text)component.findComponentById(ResourceTable.Id_title);
        searchIcon = (IMIcon)component.findComponentById(ResourceTable.Id_search);
        menuIcon = (IMIcon)component.findComponentById(ResourceTable.Id_menu);

        // 设置是否显示 返回 按钮， VISIBLE：显示，INVISIBLE：不显示，HIDE：隐藏
        if (attrSet.getAttr("back_icon").isPresent()) {
            if (attrSet.getAttr("back_icon").get().getBoolValue() == true) {
                backIcon.setVisibility(VISIBLE);
            } else {
                // 不显示
                backIcon.setVisibility(INVISIBLE);
            }
        } else {
            // 默认不显示
            backIcon.setVisibility(INVISIBLE);
        }

        // 设置标题文本
        if (attrSet.getAttr("title").isPresent()) {
            title.setText(attrSet.getAttr("title").get().getStringValue());
        } else {
            title.setText("Title");
        }

        // 设置背景颜色
        if (attrSet.getAttr("background_color").isPresent()) {
            dLayout.setBackground(attrSet.getAttr("background_color").get().getElement());
        }


    }

    private void initEvent() {
        if (backIcon != null) {
            backIcon.setClickedListener(c -> backClickedListener.onClick(c));
        }
        if (searchIcon != null) {
            searchIcon.setClickedListener(c -> searchClickedListener.onClick(c));
        }

    }
    /**
     * 设置返回按钮的监听事件
     * @param backClickedListener
     */
    public void setBackClickedListener(BackClickedListener backClickedListener) {
        this.backClickedListener = backClickedListener;
    }

    /**
     * 设置搜索按钮的监听事件
     * @param searchClickedListener
     */
    public void setSearchClickedListener(SearchClickedListener searchClickedListener) {
        this.searchClickedListener = searchClickedListener;
    }
    /**
     * 返回按钮点击事件接口
     */
    public interface BackClickedListener {
        void onClick(Component component);
    }


    public interface SearchClickedListener {
        void onClick(Component component);
    }

}
